home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / network / lattice / portlib.lzh / PORTLIB / FCRYPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-03  |  22.2 KB  |  766 lines

  1. #define BIG_ENDIAN    /* mc680x0 family */
  2.  
  3. /*
  4.  * This program is copyright (c) Alec Muffett 1991 except for certain
  5.  * portions of code ("crack-fcrypt.c") copyright (c) Robert Baldwin, Icarus
  6.  * Sparry and Alec Muffett.  The author(s) disclaims all responsibility or
  7.  * liability with respect to it's usage or its effect upon hardware or
  8.  * computer systems.  This software is in freely redistributable PROVIDED
  9.  * that this notice remains intact.
  10.  */
  11.  
  12. /*
  13.  * Misc defs for the fast password transform optimisations.
  14.  */
  15.  
  16. #ifndef LOGIN
  17. #include "crack.h"        /* contains switches - AEM */
  18. #endif
  19.  
  20. /*
  21.  * Rename the types for greater convenience ? - This is from original code.
  22.  */
  23. #define    reg    register
  24. #define    uns    unsigned
  25. #define unsb    uns char
  26. #define    unsl    uns long
  27.  
  28. /*
  29.  * Types for the different ways to represent DES bit patterns.  Bits are
  30.  * always right justified within fields.  Bits which have lower indices in
  31.  * the NBS spec are stored in the vax bits with less significance (e.g., Bit
  32.  * 1 of NBS spec is stored in the bit with weight 2 ** 0 to the Vax.
  33.  */
  34.  
  35. #define    obpb1    unsb        /* One bit per byte. */
  36. #define sbpb6    unsb        /* Six bits per byte, 6 held. */
  37. #define sbpb6R    unsb        /* Six bits per byte Reversed order, 6 held. */
  38. #define    sbpb24    unsl        /* Six bits per byte, 24 held. */
  39. #define    ebpb24    unsl        /* Eight bits per bit, 24 held. */
  40. #define    fbpb4    unsb        /* Four bits per byte, 4 held. */
  41. #define    fbpb4R    unsb        /* Four bits per byte Reversed order, 4 held. */
  42.  
  43. /*
  44.  * The operation (6 * x) is often better optimised as this (for really
  45.  * braindead compilers) - AEM
  46.  */
  47.  
  48. #ifdef BRAINDEAD6
  49. #define SIX_TIMES(exprn)        (((exprn) << 2) + ((exprn) << 1))
  50. #else
  51. #define SIX_TIMES(exprn)        (6 * (exprn))
  52. #endif                /* BRAINDEAD6 */
  53.  
  54. /*
  55.  * Data segment gathered into one place - AEM
  56.  */
  57.  
  58. /* Try to keep this stuff long aligned - AEM */
  59. static char iobuf[16];
  60.  
  61. static obpb1 crypt_block[72];    /* 72 is next multiple of 8 bytes after 66 */
  62. static sbpb24 KS[32];
  63. static sbpb24 S0H[64], S1H[64], S2H[64], S3H[64];
  64. static sbpb24 S4H[64], S5H[64], S6H[64], S7H[64];
  65. static sbpb24 S0L[64], S1L[64], S2L[64], S3L[64];
  66. static sbpb24 S4L[64], S5L[64], S6L[64], S7L[64];
  67. static sbpb24 out96[4];
  68.  
  69. /*
  70.  * These used to be rather slow and frequently used functions - AEM
  71.  */
  72.  
  73. #define TF_TO_SIXBIT(tf) \
  74.     (sbpb24)((tf & 077L) | \
  75.         ((tf & 07700L) << 2) | \
  76.         ((tf & 0770000L) << 4) | \
  77.         ((tf & 077000000L) << 6))
  78.  
  79. #define SIXBIT_TO_TF(sb) \
  80.     (ebpb24)((sb & 0x3fL) | \
  81.         ((sb & 0x3f00L) >> 2) | \
  82.         ((sb & 0x3f0000L) >> 4) | \
  83.         ((sb & 0x3f000000L) >> 6))
  84. /*
  85.  * Start of the real thing
  86.  */
  87.  
  88. void
  89. fsetkey ()
  90. {
  91.     /*
  92.      * This used to be utterly horrendous. It still is, but it's much, much,
  93.      * smaller... AEM.
  94.      */
  95.     static unsb KeyToKS[] =
  96.     {
  97.     9, 50, 33, 59, 48, 16, 32, 56, 1, 8, 18, 41, 2, 34, 25, 24,
  98.     43, 57, 58, 0, 35, 26, 17, 40, 21, 27, 38, 53, 36, 3, 46, 29,
  99.     4, 52, 22, 28, 60, 20, 37, 62, 14, 19, 44, 13, 12, 61, 54, 30,
  100.     1, 42, 25, 51, 40, 8, 24, 48, 58, 0, 10, 33, 59, 26, 17, 16,
  101.     35, 49, 50, 57, 56, 18, 9, 32, 13, 19, 30, 45, 28, 62, 38, 21,
  102.     27, 44, 14, 20, 52, 12, 29, 54, 6, 11, 36, 5, 4, 53, 46, 22,
  103.     50, 26, 9, 35, 24, 57, 8, 32, 42, 49, 59, 17, 43, 10, 1, 0,
  104.     48, 33, 34, 41, 40, 2, 58, 16, 60, 3, 14, 29, 12, 46, 22, 5,
  105.     11, 28, 61, 4, 36, 27, 13, 38, 53, 62, 20, 52, 19, 37, 30, 6,
  106.     34, 10, 58, 48, 8, 41, 57, 16, 26, 33, 43, 1, 56, 59, 50, 49,
  107.     32, 17, 18, 25, 24, 51, 42, 0, 44, 54, 61, 13, 27, 30, 6, 52,
  108.     62, 12, 45, 19, 20, 11, 60, 22, 37, 46, 4, 36, 3, 21, 14, 53,
  109.     18, 59, 42, 32, 57, 25, 41, 0, 10, 17, 56, 50, 40, 43, 34, 33,
  110.     16, 1, 2, 9, 8, 35, 26, 49, 28, 38, 45, 60, 11, 14, 53, 36,
  111.     46, 27, 29, 3, 4, 62, 44, 6, 21, 30, 19, 20, 54, 5, 61, 37,
  112.     2, 43, 26, 16, 41, 9, 25, 49, 59, 1, 40, 34, 24, 56, 18, 17,
  113.     0, 50, 51, 58, 57, 48, 10, 33, 12, 22, 29, 44, 62, 61, 37, 20,
  114.     30, 11, 13, 54, 19, 46, 28, 53, 5, 14, 3, 4, 38, 52, 45, 21,
  115.     51, 56, 10, 0, 25, 58, 9, 33, 43, 50, 24, 18, 8, 40, 2, 1,
  116.     49, 34, 35, 42, 41, 32, 59, 17, 27, 6, 13, 28, 46, 45, 21, 4,
  117.     14, 62, 60, 38, 3, 30, 12, 37, 52, 61, 54, 19, 22, 36, 29, 5,
  118.     35, 40, 59, 49, 9, 42, 58, 17, 56, 34, 8, 2, 57, 24, 51, 50,
  119.     33, 18, 48, 26, 25, 16, 43, 1, 11, 53, 60, 12, 30, 29, 5, 19,
  120.     61, 46, 44, 22, 54, 14, 27, 21, 36, 45, 38, 3, 6, 20, 13, 52,
  121.     56, 32, 51, 41, 1, 34, 50, 9, 48, 26, 0, 59, 49, 16, 43, 42,
  122.     25, 10, 40, 18, 17, 8, 35, 58, 3, 45, 52, 4, 22, 21, 60, 11,
  123.     53, 38, 36, 14, 46, 6, 19, 13, 28, 37, 30, 62, 61, 12, 5, 44,
  124.     40, 16, 35, 25, 50, 18, 34, 58, 32, 10, 49, 43, 33, 0, 56, 26,
  125.     9, 59, 24, 2, 1, 57, 48, 42, 54, 29, 36, 19, 6, 5, 44, 62,
  126.     37, 22, 20, 61, 30, 53, 3, 60, 12, 21, 14, 46, 45, 27, 52, 28,
  127.     24, 0, 48, 9, 34, 2, 18, 42, 16, 59, 33, 56, 17, 49, 40, 10,
  128.     58, 43, 8, 51, 50, 41, 32, 26, 38, 13, 20, 3, 53, 52, 28, 46,
  129.     21, 6, 4, 45, 14, 37, 54, 44, 27, 5, 61, 30, 29, 11, 36, 12,
  130.     8, 49, 32, 58, 18, 51, 2, 26, 0, 43, 17, 40, 1, 33, 24, 59,
  131.     42, 56, 57, 35, 34, 25, 16, 10, 22, 60, 4, 54, 37, 36, 12, 30,
  132.     5, 53, 19, 29, 61, 21, 38, 28, 11, 52, 45, 14, 13, 62, 20, 27,
  133.     57, 33, 16, 42, 2, 35, 51, 10, 49, 56, 1, 24, 50, 17, 8, 43,
  134.     26, 40, 41, 48, 18, 9, 0, 59, 6, 44, 19, 38, 21, 20, 27, 14,
  135.     52, 37, 3, 13, 45, 5, 22, 12, 62, 36, 29, 61, 60, 46, 4, 11,
  136.     41, 17, 0, 26, 51, 48, 35, 59, 33, 40, 50, 8, 34, 1, 57, 56,
  137.     10, 24, 25, 32, 2, 58, 49, 43, 53, 28, 3, 22, 5, 4, 11, 61,
  138.     36, 21, 54, 60, 29, 52, 6, 27, 46, 20, 13, 45, 44, 30, 19, 62,
  139.     25, 1, 49, 10, 35, 32, 48, 43, 17, 24, 34, 57, 18, 50, 41, 40,
  140.     59, 8, 9, 16, 51, 42, 33, 56, 37, 12, 54, 6, 52, 19, 62, 45,
  141.     20, 5, 38, 44, 13, 36, 53, 11, 30, 4, 60, 29, 28, 14, 3, 46,
  142.     17, 58, 41, 2, 56, 24, 40, 35, 9, 16, 26, 49, 10, 42, 33, 32,
  143.     51, 0, 1, 8, 43, 34, 25, 48, 29, 4, 46, 61, 44, 11, 54, 37,
  144.     12, 60, 30, 36, 5, 28, 45, 3, 22, 27, 52, 21, 20, 6, 62, 38
  145.     };
  146.  
  147.     reg int i;
  148.     reg int j;
  149.     reg int r;
  150.     reg unsb *k;
  151.  
  152.     k = KeyToKS;
  153.  
  154.     for (i = 0; i < 32; i++)    /* loops cache better ? - AEM */
  155.     {
  156.     r = 0;
  157.     for (j = 0; j < 24; j++)
  158.     {
  159.         r |= crypt_block[*(k++)] << j;
  160.     }
  161.     KS[i] = TF_TO_SIXBIT (r);
  162.     }
  163. }
  164.  
  165. void
  166. XForm (saltvalue)
  167.     sbpb24 saltvalue;
  168. {
  169.     union
  170.     {
  171.     sbpb24 b[2];
  172.     sbpb6 c[8];
  173.     } sdata;
  174.  
  175.     /*
  176.      * Icarus Sparry, Bath - mod AEM
  177.      */
  178.  
  179. #ifdef BIG_ENDIAN
  180. #define STEP --
  181. #define START &sdata.c[7]
  182. #define Dl sdata.b[1]
  183. #define Dh sdata.b[0]
  184. #else                /* LITTLE_ENDIAN */
  185. #define STEP ++
  186. #define START &sdata.c[0]
  187. #define Dl sdata.b[0]
  188. #define Dh sdata.b[1]
  189. #endif
  190.  
  191.     /*
  192.      * Thanks to Matt Bishop for this idea... AEM.
  193.      */
  194.  
  195. #ifndef FDES_4BYTE
  196. #define SIZEFIX        0
  197. #define INDIRECT(a,b)     (a)[b]
  198. #else
  199. #define SIZEFIX        2    /* "n" where 2^n == sizeof(sbpb24) */
  200. #define INDIRECT(a,b)     (*((sbpb24 *)(((unsigned char *) a) + (b))))
  201. #endif
  202.  
  203.     reg sbpb24 Rl;
  204.     reg sbpb24 Rh;
  205.     reg sbpb24 Ll;
  206.     reg sbpb24 Lh;
  207.     reg sbpb6 *dp;
  208.  
  209.     int loop;
  210.     reg sbpb24 k;
  211.     sbpb24 *kp;
  212.     sbpb24 *kend;
  213. #ifdef FDES_8BYTE
  214.     reg sbpb24 tmpi;
  215. #endif    /* FDES_8BYTE */
  216.  
  217.     Ll = Lh = Rl = Rh = 0;
  218.  
  219.     kend = &KS[32];
  220.  
  221.     for (loop = 25; loop-- > 0; /* nothing */ )
  222.     {
  223.     for (kp = KS; kp < kend; /* nothing */ )
  224.     {
  225.         /*
  226.          * Oddly enough, direct addressing of dp slows things down, as
  227.          * well as knackering portability - AEM
  228.          */
  229.  
  230.         k = (Rl ^ Rh) & saltvalue;
  231. #ifndef FDES_8BYTE
  232.         Dl = (k ^ Rl ^ *kp++) << SIZEFIX;
  233.         Dh = (k ^ Rh ^ *kp++) << SIZEFIX;
  234. #else
  235.         /* hack to make things work better - matthew kaufman */
  236.         /* I haven't tried any of this - I don't have a cray... AEM */
  237.         tmpi = (k ^ Rl ^ *kp++);
  238.         sdata.c[3] = (tmpi >> 24) & 0x00ff;
  239.         sdata.c[2] = (tmpi >> 16) & 0x00ff;
  240.         sdata.c[1] = (tmpi >> 8) & 0x00ff;
  241.         sdata.c[0] = (tmpi) & 0x00ff;
  242.         tmpi = (k ^ Rh ^ *kp++);
  243.         sdata.c[7] = (tmpi >> 24) & 0x00ff;
  244.         sdata.c[6] = (tmpi >> 16) & 0x00ff;
  245.         sdata.c[5] = (tmpi >> 8) & 0x00ff;
  246.         sdata.c[4] = (tmpi) & 0x00ff;
  247. #endif    /* FDES_8BYTE */
  248.  
  249.         dp = START;
  250.         Lh ^= INDIRECT (S0H, *dp);
  251.         Ll ^= INDIRECT (S0L, *dp STEP);
  252.         Lh ^= INDIRECT (S1H, *dp);
  253.         Ll ^= INDIRECT (S1L, *dp STEP);
  254.         Lh ^= INDIRECT (S2H, *dp);
  255.         Ll ^= INDIRECT (S2L, *dp STEP);
  256.         Lh ^= INDIRECT (S3H, *dp);
  257.         Ll ^= INDIRECT (S3L, *dp STEP);
  258.         Lh ^= INDIRECT (S4H, *dp);
  259.         Ll ^= INDIRECT (S4L, *dp STEP);
  260.         Lh ^= INDIRECT (S5H, *dp);
  261.         Ll ^= INDIRECT (S5L, *dp STEP);
  262.         Lh ^= INDIRECT (S6H, *dp);
  263.         Ll ^= INDIRECT (S6L, *dp STEP);
  264.         Lh ^= INDIRECT (S7H, *dp);
  265.         Ll ^= INDIRECT (S7L, *dp STEP);
  266.  
  267.         k = (Ll ^ Lh) & saltvalue;
  268. #ifndef FDES_8BYTE
  269.         Dl = (k ^ Ll ^ *kp++) << SIZEFIX;
  270.         Dh = (k ^ Lh ^ *kp++) << SIZEFIX;
  271. #else
  272.         tmpi = (k ^ Ll ^ *kp++);
  273.         sdata.c[3] = (tmpi >> 24) & 0x00ff;
  274.         sdata.c[2] = (tmpi >> 16) & 0x00ff;
  275.         sdata.c[1] = (tmpi >> 8) & 0x00ff;
  276.         sdata.c[0] = (tmpi) & 0x00ff;
  277.         tmpi = (k ^ Lh ^ *kp++);
  278.         sdata.c[7] = (tmpi >> 24) & 0x00ff;
  279.         sdata.c[6] = (tmpi >> 16) & 0x00ff;
  280.         sdata.c[5] = (tmpi >> 8) & 0x00ff;
  281.         sdata.c[4] = (tmpi) & 0x00ff;
  282. #endif    /* FDES_8BYTE */
  283.  
  284.         dp = START;
  285.         Rh ^= INDIRECT (S0H, *dp);
  286.         Rl ^= INDIRECT (S0L, *dp STEP);
  287.         Rh ^= INDIRECT (S1H, *dp);
  288.         Rl ^= INDIRECT (S1L, *dp STEP);
  289.         Rh ^= INDIRECT (S2H, *dp);
  290.         Rl ^= INDIRECT (S2L, *dp STEP);
  291.         Rh ^= INDIRECT (S3H, *dp);
  292.         Rl ^= INDIRECT (S3L, *dp STEP);
  293.         Rh ^= INDIRECT (S4H, *dp);
  294.         Rl ^= INDIRECT (S4L, *dp STEP);
  295.         Rh ^= INDIRECT (S5H, *dp);
  296.         Rl ^= INDIRECT (S5L, *dp STEP);
  297.         Rh ^= INDIRECT (S6H, *dp);
  298.         Rl ^= INDIRECT (S6L, *dp STEP);
  299.         Rh ^= INDIRECT (S7H, *dp);
  300.         Rl ^= INDIRECT (S7L, *dp STEP);
  301.     }
  302.  
  303.     Ll ^= Rl;
  304.     Lh ^= Rh;
  305.     Rl ^= Ll;
  306.     Rh ^= Lh;
  307.     Ll ^= Rl;
  308.     Lh ^= Rh;
  309.     }
  310.  
  311.     /*
  312.      * for reasons that I cannot explain, if I insert the contents of the
  313.      * UnXForm function right HERE, making the tweaks as necessary to avoid
  314.      * using out96[] to pass data, I LOSE 30% of my speed.  I don't know why.
  315.      * Hence, I continue to use out96[]...
  316.      */
  317.     {
  318.     reg sbpb24 *qp;
  319.     qp = out96;
  320.     *qp++ = Ll;
  321.     *qp++ = Lh;
  322.     *qp++ = Rl;
  323.     *qp++ = Rh;
  324.     }
  325. }
  326.  
  327. void
  328. UnXForm ()
  329. {
  330.     reg obpb1 *ptr;
  331.     reg sbpb24 Rl;
  332.     reg sbpb24 Rh;
  333.     reg sbpb24 Ll;
  334.     reg sbpb24 Lh;
  335.  
  336.     Ll = SIXBIT_TO_TF (out96[0]);
  337.     Lh = SIXBIT_TO_TF (out96[1]);
  338.     Rl = SIXBIT_TO_TF (out96[2]);
  339.     Rh = SIXBIT_TO_TF (out96[3]);
  340.     ptr = crypt_block;
  341.  
  342.     *(ptr++) = Rl & 0x000400L ? 0x01 : 0;
  343.     *(ptr++) = Ll & 0x000400L ? 0x01 : 0;
  344.     *(ptr++) = Rl & 0x400000L ? 0x01 : 0;
  345.     *(ptr++) = Ll & 0x400000L ? 0x01 : 0;
  346.     *(ptr++) = Rh & 0x000400L ? 0x01 : 0;
  347.     *(ptr++) = Lh & 0x000400L ? 0x01 : 0;
  348.     *(ptr++) = Rh & 0x400000L ? 0x01 : 0;
  349.     *(ptr++) = Lh & 0x400000L ? 0x01 : 0;
  350.     *(ptr++) = Rl & 0x000200L ? 0x01 : 0;
  351.     *(ptr++) = Ll & 0x000200L ? 0x01 : 0;
  352.     *(ptr++) = Rl & 0x200000L ? 0x01 : 0;
  353.     *(ptr++) = Ll & 0x200000L ? 0x01 : 0;
  354.     *(ptr++) = Rh & 0x000200L ? 0x01 : 0;
  355.     *(ptr++) = Lh & 0x000200L ? 0x01 : 0;
  356.     *(ptr++) = Rh & 0x200000L ? 0x01 : 0;
  357.     *(ptr++) = Lh & 0x200000L ? 0x01 : 0;
  358.     *(ptr++) = Rl & 0x000100L ? 0x01 : 0;
  359.     *(ptr++) = Ll & 0x000100L ? 0x01 : 0;
  360.     *(ptr++) = Rl & 0x100000L ? 0x01 : 0;
  361.     *(ptr++) = Ll & 0x100000L ? 0x01 : 0;
  362.     *(ptr++) = Rh & 0x000100L ? 0x01 : 0;
  363.     *(ptr++) = Lh & 0x000100L ? 0x01 : 0;
  364.     *(ptr++) = Rh & 0x100000L ? 0x01 : 0;
  365.     *(ptr++) = Lh & 0x100000L ? 0x01 : 0;
  366.     *(ptr++) = Rl & 0x000080L ? 0x01 : 0;
  367.     *(ptr++) = Ll & 0x000080L ? 0x01 : 0;
  368.     *(ptr++) = Rl & 0x080000L ? 0x01 : 0;
  369.     *(ptr++) = Ll & 0x080000L ? 0x01 : 0;
  370.     *(ptr++) = Rh & 0x000080L ? 0x01 : 0;
  371.     *(ptr++) = Lh & 0x000080L ? 0x01 : 0;
  372.     *(ptr++) = Rh & 0x080000L ? 0x01 : 0;
  373.     *(ptr++) = Lh & 0x080000L ? 0x01 : 0;
  374.     *(ptr++) = Rl & 0x000010L ? 0x01 : 0;
  375.     *(ptr++) = Ll & 0x000010L ? 0x01 : 0;
  376.     *(ptr++) = Rl & 0x010000L ? 0x01 : 0;
  377.     *(ptr++) = Ll & 0x010000L ? 0x01 : 0;
  378.     *(ptr++) = Rh & 0x000010L ? 0x01 : 0;
  379.     *(ptr++) = Lh & 0x000010L ? 0x01 : 0;
  380.     *(ptr++) = Rh & 0x010000L ? 0x01 : 0;
  381.     *(ptr++) = Lh & 0x010000L ? 0x01 : 0;
  382.     *(ptr++) = Rl & 0x000008L ? 0x01 : 0;
  383.     *(ptr++) = Ll & 0x000008L ? 0x01 : 0;
  384.     *(ptr++) = Rl & 0x008000L ? 0x01 : 0;
  385.     *(ptr++) = Ll & 0x008000L ? 0x01 : 0;
  386.     *(ptr++) = Rh & 0x000008L ? 0x01 : 0;
  387.     *(ptr++) = Lh & 0x000008L ? 0x01 : 0;
  388.     *(ptr++) = Rh & 0x008000L ? 0x01 : 0;
  389.     *(ptr++) = Lh & 0x008000L ? 0x01 : 0;
  390.     *(ptr++) = Rl & 0x000004L ? 0x01 : 0;
  391.     *(ptr++) = Ll & 0x000004L ? 0x01 : 0;
  392.     *(ptr++) = Rl & 0x004000L ? 0x01 : 0;
  393.     *(ptr++) = Ll & 0x004000L ? 0x01 : 0;
  394.     *(ptr++) = Rh & 0x000004L ? 0x01 : 0;
  395.     *(ptr++) = Lh & 0x000004L ? 0x01 : 0;
  396.     *(ptr++) = Rh & 0x004000L ? 0x01 : 0;
  397.     *(ptr++) = Lh & 0x004000L ? 0x01 : 0;
  398.     *(ptr++) = Rl & 0x000002L ? 0x01 : 0;
  399.     *(ptr++) = Ll & 0x000002L ? 0x01 : 0;
  400.     *(ptr++) = Rl & 0x002000L ? 0x01 : 0;
  401.     *(ptr++) = Ll & 0x002000L ? 0x01 : 0;
  402.     *(ptr++) = Rh & 0x000002L ? 0x01 : 0;
  403.     *(ptr++) = Lh & 0x000002L ? 0x01 : 0;
  404.     *(ptr++) = Rh & 0x002000L ? 0x01 : 0;
  405.     *ptr = Lh & 0x002000L ? 0x01 : 0;
  406. }
  407.  
  408. #ifdef LOGIN
  409. static int _fcrypt_initialised = 0;
  410. #endif
  411.  
  412. #ifdef MINT
  413. char *
  414. crypt (pw, salt)
  415. #else
  416. char *
  417. fcrypt (pw, salt)
  418. #endif
  419.     char *pw;
  420.     char *salt;
  421. {
  422.     /* Table lookups for salts reduce fcrypt() overhead dramatically */
  423.     static sbpb24 salt0[] =
  424.     {
  425.     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
  426.     34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  427.     50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 1,
  428.     2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10,
  429.     11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  430.     27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 32, 33, 34, 35, 36,
  431.     37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  432.     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 1, 2, 3, 4,
  433.     5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  434.     21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
  435.     37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  436.     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 1, 2, 3, 4,
  437.     5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  438.     21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
  439.     37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  440.     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 1, 2, 3, 4
  441.     };
  442.     static sbpb24 salt1[] =
  443.     {
  444.     1152, 1216, 1280, 1344, 1408, 1472, 1536, 1600,
  445.     1664, 1728, 1792, 1856, 1920, 1984, 2048, 2112,
  446.     2176, 2240, 2304, 2368, 2432, 2496, 2560, 2624,
  447.     2688, 2752, 2816, 2880, 2944, 3008, 3072, 3136,
  448.     3200, 3264, 3328, 3392, 3456, 3520, 3584, 3648,
  449.     3712, 3776, 3840, 3904, 3968, 4032, 0, 64,
  450.     128, 192, 256, 320, 384, 448, 512, 576,
  451.     640, 704, 320, 384, 448, 512, 576, 640,
  452.     704, 768, 832, 896, 960, 1024, 1088, 1152,
  453.     1216, 1280, 1344, 1408, 1472, 1536, 1600, 1664,
  454.     1728, 1792, 1856, 1920, 1984, 2048, 2112, 2176,
  455.     2240, 2304, 2368, 2048, 2112, 2176, 2240, 2304,
  456.     2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816,
  457.     2880, 2944, 3008, 3072, 3136, 3200, 3264, 3328,
  458.     3392, 3456, 3520, 3584, 3648, 3712, 3776, 3840,
  459.     3904, 3968, 4032, 0, 64, 128, 192, 256,
  460.     320, 384, 448, 512, 576, 640, 704, 768,
  461.     832, 896, 960, 1024, 1088, 1152, 1216, 1280,
  462.     1344, 1408, 1472, 1536, 1600, 1664, 1728, 1792,
  463.     1856, 1920, 1984, 2048, 2112, 2176, 2240, 2304,
  464.     2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816,
  465.     2880, 2944, 3008, 3072, 3136, 3200, 3264, 3328,
  466.     3392, 3456, 3520, 3584, 3648, 3712, 3776, 3840,
  467.     3904, 3968, 4032, 0, 64, 128, 192, 256,
  468.     320, 384, 448, 512, 576, 640, 704, 768,
  469.     832, 896, 960, 1024, 1088, 1152, 1216, 1280,
  470.     1344, 1408, 1472, 1536, 1600, 1664, 1728, 1792,
  471.     1856, 1920, 1984, 2048, 2112, 2176, 2240, 2304,
  472.     2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816,
  473.     2880, 2944, 3008, 3072, 3136, 3200, 3264, 3328,
  474.     3392, 3456, 3520, 3584, 3648, 3712, 3776, 3840,
  475.     3904, 3968, 4032, 0, 64, 128, 192, 256
  476.     };
  477.  
  478.     /* final perutation desalting */
  479.     static obpb1 final[] =
  480.     {
  481.     46, 47, 48, 49, 50, 51, 52, 53,
  482.     54, 55, 56, 57, 65, 66, 67, 68,
  483.     69, 70, 71, 72, 73, 74, 75, 76,
  484.     77, 78, 79, 80, 81, 82, 83, 84,
  485.     85, 86, 87, 88, 89, 90, 97, 98,
  486.     99, 100, 101, 102, 103, 104, 105, 106,
  487.     107, 108, 109, 110, 111, 112, 113, 114,
  488.     115, 116, 117, 118, 119, 120, 121, 122,
  489.     123, 124, 125, 126, 127, 128, 129, 130,
  490.     131, 132, 133, 134, 135, 136, 137, 138,
  491.     139, 140, 141, 142, 143, 144, 145, 146,
  492.     147, 148, 149, 150, 151, 152, 153, 154,
  493.     155, 156, 157, 158, 159, 160, 161, 162,
  494.     163, 164, 165, 166, 167, 168, 169, 170,
  495.     171, 172, 173, 174, 175, 176, 177, 178,
  496.     179, 180, 181, 182, 183, 184, 185, 186,
  497.     187, 188, 189, 190, 191, 192, 193, 194,
  498.     195, 196, 197, 198, 199, 200, 201, 202,
  499.     203, 204, 205, 206, 207, 208, 209, 210,
  500.     211, 212, 213, 214, 215, 216, 217, 218,
  501.     219, 220, 221, 222, 223, 224, 225, 226,
  502.     227, 228, 229, 230, 231, 232, 233, 234,
  503.     235, 236, 237, 238, 239, 240, 241, 242,
  504.     243, 244, 245, 246, 247, 248, 249, 250,
  505.     251, 252, 253, 254, 255,
  506.     /* Truncate overflow bits at 256 */
  507.     0, 1, 2, 3, 4, 5, 6, 7,
  508.     8, 9, 10, 11, 12, 13, 14, 15,
  509.     16, 17, 18, 19, 20, 21, 22, 23,
  510.     24, 25, 26, 27, 28, 29, 30, 31,
  511.     32, 33, 34, 35, 36, 37, 38, 39,
  512.     40, 41, 42, 43, 44, 45, 46, 47,
  513.     48, 49, 50, 51, 52, 53, 54, 55,
  514.     56, 57, 58
  515.     };
  516.  
  517.     reg int i, j, k;
  518.     reg long int *lip;
  519.     sbpb24 saltvalue;
  520.  
  521. #ifdef LOGIN
  522.     if (_fcrypt_initialised == 0)
  523.     {
  524.         init_des();
  525.     _fcrypt_initialised = 1;
  526.     }
  527. #endif
  528.  
  529. #ifdef BUILTIN_CLEAR
  530.     lip = (long int *) crypt_block;
  531.     for (i = (sizeof (crypt_block) / sizeof (long int)); i > 0; i--)
  532.     {
  533.     *(lip++) = 0L;
  534.     }
  535. #else                /* BUILTIN_CLEAR */
  536. #ifdef BZERO
  537.     bzero (crypt_block, 66);
  538. #else                /* BZERO */
  539.     for (i = 0; i < 66; i++)
  540.     {
  541.     crypt_block[i] = '\0';
  542.     }
  543. #endif                /* BZERO */
  544. #endif                /* BUILTIN_CLEAR */
  545.  
  546.     for (i = 0; (k = *pw) && i < 64; pw++)
  547.     {
  548.     crypt_block[i++] = (k >> 6) & 01;
  549.     crypt_block[i++] = (k >> 5) & 01;
  550.     crypt_block[i++] = (k >> 4) & 01;
  551.     crypt_block[i++] = (k >> 3) & 01;
  552.     crypt_block[i++] = (k >> 2) & 01;
  553.     crypt_block[i++] = (k >> 1) & 01;
  554.     crypt_block[i++] = (k >> 0) & 01;
  555.     i++;            /* have to skip one here (parity bit) */
  556.     }
  557.  
  558.     fsetkey ( /* crypt_block */ );
  559.  
  560. #ifdef BUILTIN_CLEAR
  561.     lip = (long int *) crypt_block;
  562.     for (i = (sizeof (crypt_block) / sizeof (long int)); i > 0; i--)
  563.     {
  564.     *(lip++) = 0L;
  565.     }
  566. #else                /* BUILTIN_CLEAR */
  567. #ifdef BZERO
  568.     bzero (crypt_block, 66);
  569. #else                /* BZERO */
  570.     for (i = 0; i < 66; i++)
  571.     {
  572.     crypt_block[i] = '\0';
  573.     }
  574. #endif                /* BZERO */
  575. #endif                /* BUILTIN_CLEAR */
  576.  
  577.     iobuf[0] = salt[0];
  578.     iobuf[1] = salt[1];
  579.  
  580.     saltvalue = salt0[iobuf[0]] | salt1[iobuf[1]];
  581.     saltvalue = TF_TO_SIXBIT (saltvalue);
  582.  
  583.     XForm (saltvalue);
  584.     UnXForm ();
  585.  
  586.     for (i = 0; i < 11; i++)
  587.     {
  588.     k = 0;
  589.  
  590.     for (j = 0; j < 6; j++)
  591.     {
  592.         k = (k << 1) | crypt_block[SIX_TIMES (i) + j];
  593.     }
  594.     iobuf[i + 2] = final[k];
  595.     }
  596.  
  597.     iobuf[i + 2] = 0;
  598.  
  599.     if (iobuf[1] == 0)
  600.     {
  601.     iobuf[1] = iobuf[0];
  602.     }
  603.     return (iobuf);
  604. }
  605. /********* INITIALISATION ROUTINES *********/
  606.  
  607. fbpb4
  608. lookupS (tableno, t6bits)
  609.     unsl tableno;
  610.     sbpb6R t6bits;
  611. {
  612.     static fbpb4R S[8][64] =
  613.     {
  614.     14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  615.     0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  616.     4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  617.     15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
  618.  
  619.     15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  620.     3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  621.     0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  622.     13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
  623.  
  624.     10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  625.     13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  626.     13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  627.     1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
  628.  
  629.     7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  630.     13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  631.     10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  632.     3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
  633.  
  634.     2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  635.     14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  636.     4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  637.     11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
  638.  
  639.     12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  640.     10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  641.     9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  642.     4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
  643.  
  644.     4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  645.     13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  646.     1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  647.     6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
  648.  
  649.     13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  650.     1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  651.     7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  652.     2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11,
  653.     };
  654.     sbpb6 fixed6bits;
  655.     fbpb4R r;
  656.     fbpb4 fixedr;
  657.  
  658.     fixed6bits = (((t6bits >> 0) & 01) << 5) +
  659.     (((t6bits >> 1) & 01) << 3) +
  660.     (((t6bits >> 2) & 01) << 2) +
  661.     (((t6bits >> 3) & 01) << 1) +
  662.     (((t6bits >> 4) & 01) << 0) +
  663.     (((t6bits >> 5) & 01) << 4);
  664.  
  665.     r = S[tableno][fixed6bits];
  666.  
  667.     fixedr = (((r >> 3) & 01) << 0) +
  668.     (((r >> 2) & 01) << 1) +
  669.     (((r >> 1) & 01) << 2) +
  670.     (((r >> 0) & 01) << 3);
  671.  
  672.     return (fixedr);
  673. }
  674.  
  675. void
  676. init (tableno, lowptr, highptr)
  677.     unsl tableno;
  678.     sbpb24 *lowptr, *highptr;
  679. {
  680.  
  681.     static unsb P[] =
  682.     {
  683.     15, 6, 19, 20,
  684.     28, 11, 27, 16,
  685.     0, 14, 22, 25,
  686.     4, 17, 30, 9,
  687.     1, 7, 23, 13,
  688.     31, 26, 2, 8,
  689.     18, 12, 29, 5,
  690.     21, 10, 3, 24,
  691.     };
  692.  
  693.     static unsb E[] =
  694.     {
  695.     31, 0, 1, 2, 3, 4,
  696.     3, 4, 5, 6, 7, 8,
  697.     7, 8, 9, 10, 11, 12,
  698.     11, 12, 13, 14, 15, 16,
  699.     15, 16, 17, 18, 19, 20,
  700.     19, 20, 21, 22, 23, 24,
  701.     23, 24, 25, 26, 27, 28,
  702.     27, 28, 29, 30, 31, 0,
  703.     };
  704.  
  705.     static obpb1 tmp32[32];
  706.     static obpb1 tmpP32[32];
  707.     static obpb1 tmpE[48];
  708.  
  709.     int j, k, i;
  710.     int tablenoX4;
  711.     reg sbpb24 spare24;
  712.  
  713.     tablenoX4 = tableno * 4;
  714.  
  715.     for (j = 0; j < 64; j++)
  716.     {
  717.     k = lookupS (tableno, j);
  718.  
  719.     for (i = 0; i < 32; i++)
  720.     {
  721.         tmp32[i] = 0;
  722.     }
  723.     for (i = 0; i < 4; i++)
  724.     {
  725.         tmp32[tablenoX4 + i] = (k >> i) & 01;
  726.     }
  727.     for (i = 0; i < 32; i++)
  728.     {
  729.         tmpP32[i] = tmp32[P[i]];
  730.     }
  731.     for (i = 0; i < 48; i++)
  732.     {
  733.         tmpE[i] = tmpP32[E[i]];
  734.     }
  735.  
  736.     lowptr[j] = 0;
  737.     highptr[j] = 0;
  738.  
  739.     for (i = 0; i < 24; i++)
  740.     {
  741.         lowptr[j] |= tmpE[i] << i;
  742.     }
  743.     for (k = 0, i = 24; i < 48; i++, k++)
  744.     {
  745.         highptr[j] |= tmpE[i] << k;
  746.     }
  747.  
  748.     spare24 = lowptr[j];    /* to allow for macro expansion */
  749.     lowptr[j] = TF_TO_SIXBIT (spare24);
  750.     spare24 = highptr[j];    /* to allow for macro expansion */
  751.     highptr[j] = TF_TO_SIXBIT (spare24);
  752.     }
  753. }
  754. init_des ()
  755. {
  756.     init (0, S0L, S0H);
  757.     init (1, S1L, S1H);
  758.     init (2, S2L, S2H);
  759.     init (3, S3L, S3H);
  760.     init (4, S4L, S4H);
  761.     init (5, S5L, S5H);
  762.     init (6, S6L, S6H);
  763.     init (7, S7L, S7H);
  764. }
  765.  
  766.